import 'package:flutter/material.dart';
import 'package:flutter_love/bean/banner_bean.dart';
import 'package:flutter_love/bean/detail_bean.dart';
import 'package:flutter_love/generated/json/base/json_convert_content.dart';
import 'package:flutter_love/global/global.dart';
import 'package:flutter_love/net/dio_manager.dart';
import 'package:flutter_love/utils/page_route_util.dart';
import 'package:flutter_love/utils/progress_dialog_util.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class CartView extends StatefulWidget {
  const CartView({Key? key}) : super(key: key);

  @override
  _CartViewState createState() => _CartViewState();
}

class _CartViewState extends State<CartView> {
  List bannerList = [];

  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(milliseconds: 200)).then((e) {
      setState(() {
        ProgressDialogUtil.showProgress(context);
        DioManager.getInstance()?.get('/banner/json', {}, (data) {
          setState(() {
            ProgressDialogUtil.dismiss(context);
            BannerBean bannerBean = JsonConvert.fromJsonAsT(data);
            bannerList.clear();
            bannerList.addAll(bannerBean.data);
          });
        }, (error) {});
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('购物车'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          SizedBox(
            height: 170,
            width: double.infinity,
            child: Swiper(
              onTap: (index) {
                BannerData data = bannerList[index];
                DetailBean bean = DetailBean();
                bean.title = data.title;
                bean.url = data.url;
                pushNameAndArguments(context, Global.routeWebDetail, bean);
              },
              itemBuilder: _swiperBuilder,
              itemCount: bannerList.length,
              autoplay: true,
              key: UniqueKey(),
              scrollDirection: Axis.horizontal,
              loop: true,
              duration: 1000,
              pagination: const SwiperPagination(
                  builder: DotSwiperPaginationBuilder(
                color: Colors.red,
                activeColor: Colors.white,
              )),
            ),
          ),
        ],
      ),
    );
  }

  Widget _swiperBuilder(BuildContext context, int index) {
    BannerData data = bannerList[index];
    return (Image.network(
      data.imagePath,
      fit: BoxFit.fill,
    ));
  }
}
